//
//  RegisterViewController.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/15.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "PhoneLoginVC.h"
#import "AppHeader.h"
#import "HomeTabBarVC.h"
#import "HomeViewController.h"
#import "LoginViewController.h"

@interface PhoneLoginVC () {
    
    __weak IBOutlet NSLayoutConstraint *topSpace;
}

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (nonatomic, strong) UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) UIWindow *window;


@end

@implementation PhoneLoginVC


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
/// 强制状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (IS_IPHONE_6PLUS && IS_IPHONE_6) {
        topSpace.constant =80;
    } else if (IS_IPHONE_X) {
        topSpace.constant =80;
    }
    
    [self initCodeButton];
    
}
- (void)initCodeButton {
    UIView *codeRightView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, CGRectGetHeight(_phoneTF.frame))];
    UIButton *codeButton =[UIButton buttonWithType:UIButtonTypeCustom];
    codeButton.frame =CGRectMake(10, (CGRectGetHeight(_phoneTF.frame)-33)/2.0, 90, 33);
    [codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    codeButton.titleLabel.font =[UIFont systemFontOfSize:15];
    [codeButton setTitleColor:APP_DEFAULT_COLOUR forState:UIControlStateNormal];
    codeButton.cornerColor =APP_DEFAULT_COLOUR;
    codeButton.cornerRadius =5.0;
    [codeButton addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [codeRightView addSubview:codeButton];
    self.codeButton =codeButton;
    self.codeTF.rightView =codeRightView;
    self.codeTF.rightViewMode =UITextFieldViewModeAlways;
}

- (void)codeButtonClick:(UIButton*)button {
//    ShowAlertTitle(@"网络或服务器错误,请稍后再试");
    if (![self.phoneTF.text validateMobile]) {
        //        [AppToast showFileWithString:@"您输入的手机号合法"];
        [TheGlobalMethod xianShiAlertView:@"您输入的手机号合法" controller:self];
    }
    else
    {
    [HttpUrl GET:@"user/getPhoneCode"dict:@{@"phone":_phoneTF.text} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
              [self startCountdown];
            }
            
        else{
            [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];
        }
    }];
    }
}

- (void)startCountdown {
    [TimerHandler countdownSecond:60 eventHandler:^(dispatch_source_t timer, NSInteger surplusTime) {
        if (surplusTime ==0) {
            self.codeButton.enabled =YES;
            [self.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
        } else {
            if (self.codeButton.isEnabled) {
                self.codeButton.enabled =NO;
            }
            [self.codeButton setTitle:[NSString stringWithFormat:@"%ld秒后再试", surplusTime] forState:UIControlStateNormal];
        }
    }];
}

- (IBAction)loginButtonClick:(UIButton *)sender {
    if (![self.phoneTF.text validateMobile]) {
//        [AppToast showFileWithString:@"您输入的手机号合法"];
        [TheGlobalMethod xianShiAlertView:@"您输入的手机号合法" controller:self];
    }
    else if(self.codeTF.text.length ==0) {
//        [AppToast showFileWithString:@"请输入短信验证码"];
         [TheGlobalMethod xianShiAlertView:@"请输入短信验证码" controller:self];
    }
    else
    {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [HttpUrl GET:@"user/register"dict:@{@"phone":_phoneTF.text,@"code":_codeTF.text} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            [HttpUrl GET:@"user/loginPhone"dict:@{@"phone":_phoneTF.text,@"code":_codeTF.text} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
                if (BB_isSuccess) {
                    [TheGlobalMethod insertUserDefault:_phoneTF.text Key:@"phone"];
                    [TheGlobalMethod insertUserDefault:_codeTF.text Key:@"code"];
                    [TheGlobalMethod insertUserDefault:[data[@"data"][@"token"] description] Key:@"token"];
                    [TheGlobalMethod insertUserDefault:[data[@"data"][@"headImg"] description] Key:@"headImg"];
                    [TheGlobalMethod insertUserDefault:[data[@"data"][@"userName"] description] Key:@"userName"];
                    [TheGlobalMethod insertUserDefault:[data[@"data"][@"id"] description] Key:@"usrid"];
                    HomeTabBarVC *tabBarVC =[[HomeTabBarVC alloc]init];
                    self.window.rootViewController = tabBarVC;
                    [self.window makeKeyAndVisible];
                }
                else{
                    [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];
                }
            }];
        }
        else{
            if([[data[@"msg"] description] containsString:@"该号码已被注册"])
            {
                [HttpUrl GET:@"user/loginPhone"dict:@{@"phone":_phoneTF.text,@"code":_codeTF.text} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
                    if (BB_isSuccess) {
                        [TheGlobalMethod insertUserDefault:_phoneTF.text Key:@"phone"];
                        [TheGlobalMethod insertUserDefault:_codeTF.text Key:@"code"];
                        [TheGlobalMethod insertUserDefault:[data[@"data"][@"token"] description] Key:@"token"];
                        [TheGlobalMethod insertUserDefault:[data[@"data"][@"headImg"] description] Key:@"headImg"];
                        [TheGlobalMethod insertUserDefault:[data[@"data"][@"userName"] description] Key:@"userName"];
                        [TheGlobalMethod insertUserDefault:[data[@"data"][@"id"] description] Key:@"usrid"];
                        HomeTabBarVC *tabBarVC =[[HomeTabBarVC alloc]init];
                        self.window.rootViewController = tabBarVC;
                        [self.window makeKeyAndVisible];
                    }
                    else{
                        [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];
                    }
                }];
            }
            else
            {
                [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];

            }
        }
    }];
    }
}

- (IBAction)protocolButtonClick:(UIButton *)sender {
    QZMessageDetailVC *messageVC =[[QZMessageDetailVC alloc]init];
    messageVC.title =@"用户协议";
    messageVC.hidesBottomBarWhenPushed =YES;
    messageVC.htmlStr =@"使用协议";
    [self.navigationController pushViewController:messageVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
